package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;



    /**
     * 添加套餐信息
     * @param setmeal
     * @return
     */
    @Transactional
    public void add(Setmeal setmeal,Integer[] checkgruopIds) {
        setmealMapper.insert(setmeal);
        Integer setmealId = setmeal.getId();
        if (checkgruopIds != null && checkgruopIds.length > 0){
            for (Integer checkgruopId : checkgruopIds){
                setmealMapper.setmealAndCheckGroup(setmealId,checkgruopId);
            }
        }
    }

    /**
     * 分页查询套餐信息
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealMapper.findPage(queryPageBean);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    /**
     *通过id查询套餐信息
     * @param id
     * @return
     */
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealMapper.findById(id);
        return setmeal;
    }

    /**
     * 删除套餐信息
     * @param id
     */
    @Transactional
    public void delete(Integer id) {
        setmealMapper.delete(id);
    }

    /**
     * 套餐占比
     *
     * @return
     */
    public List<Map<String, Object>> findSetmealCount() {
        List<Map<String, Object>> list = setmealMapper.findSetmealCount();
        return list;
    }

    /**
     * 编辑套餐信息
     * @param setmeal
     */
    @Transactional
    public void edit(Setmeal setmeal,Integer[] checkgruopIds) {
        setmealMapper.edit(setmeal);
        Integer setmealId = setmeal.getId();
        if (checkgruopIds != null && checkgruopIds.length > 0){
            for (Integer checkgruopId : checkgruopIds){
                setmealMapper.updatesetmealAndCheckGroup(setmealId,checkgruopId);
            }
        }
    }

    /**
     *
     * @return
     */
    public List<Setmeal> findAll() {
        List<Setmeal> list = setmealMapper.findAll();
        return list;
    }

    @Override
    public Setmeal findinfoById(Integer mealId) {
        Setmeal  setmeal = setmealMapper.findinfoById(mealId);
        return setmeal;
    }


}
